package com.exartisan.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.exartisan.bean.Post;
import com.exartisan.thanhdoanhn.DetailNormalActivity;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.CommentDialog;
import com.exartisan.util.CommentDialog.onCommentListener;
import com.exartisan.util.DataHolder;
import com.exartisan.util.DateTimeFormat;
import com.exartisan.util.FbUtil;
import com.exartisan.util.Util;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PostAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Post> posts;
	private HashMap<String, Integer> likeCount;
	private HashMap<String, Integer> commentCount;

	public PostAdapter(Context context, ArrayList<Post> posts) {
		this.context = context;
		this.posts = posts;
		likeCount = new HashMap<String, Integer>();
		commentCount = new HashMap<String, Integer>();
	}

	@Override
	public int getCount() {
		return posts.size();
	}

	@Override
	public Object getItem(int pos) {
		return posts.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return posts.get(pos).getPostID();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		final ViewHolder holder;
		View row;
		if (convertView == null) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			holder = new ViewHolder();

			// set row layout
			row = inflater.inflate(R.layout.row_normal_list, null);

			holder.imgAvatar = (ImageView) row.findViewById(R.id.imgCreator);
			holder.imgDesc = (ImageView) row.findViewById(R.id.imgDescription);
			holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
			holder.tvDesc = (TextView) row.findViewById(R.id.tvDescription);
			holder.tvUserName = (TextView) row.findViewById(R.id.tvCreator);
			holder.tvCreatedTime = (TextView) row
					.findViewById(R.id.tvCreatedDate);
			holder.tvLikes = (TextView) row.findViewById(R.id.tvLikes);
			holder.tvComments = (TextView) row.findViewById(R.id.tvComments);
			holder.tvView = (TextView) row.findViewById(R.id.tvViewsBlog);
			holder.layoutLikeBlog=(RelativeLayout) row.findViewById(R.id.layoutLikeBlog);
			holder.layoutCommentBlog = (RelativeLayout) row.findViewById(R.id.layoutCommentBlog);
			holder.btnLike=(ImageView) row.findViewById(R.id.btnLike);
			row.setTag(holder);

		} else {
			row = (View) convertView;
			holder = (ViewHolder) row.getTag();
		}

		final Post post = posts.get(position);

		// holder.imgAvatar.setImageUrl(post.getUserAvatarUrl());
		// holder.imgDesc.setImageUrl(post.getThumbURL());

		ImageLoader.getInstance().displayImage(post.getThumbURL(),
				holder.imgDesc);
		ImageLoader.getInstance().displayImage(post.getUserAvatarUrl(),
				holder.imgAvatar);

		holder.tvTitle.setText(post.getTitle());
		holder.tvDesc.setText(post.getDescription());
		holder.tvUserName.setText(post.getUserName());
		holder.tvCreatedTime.setText(DateTimeFormat.format(
				post.getCreateDate(), DateTimeFormat.SHORT_VALUE));
		holder.tvView.setText(post.getViews() + "");
		
		if(post.isLiked()) {
			holder.btnLike.setBackgroundResource(R.drawable.like_red);
			holder.tvLikes.setTextColor(Color.parseColor("#bf0000"));
		} else {
			holder.btnLike.setBackgroundResource(R.drawable.like_black);
			holder.tvLikes.setTextColor(Color.parseColor("#000000"));
		}
		
		if (likeCount.containsKey(post.getFbFeedID()))
			if(likeCount.get(post.getFbFeedID())!=0) holder.tvLikes.setText(likeCount.get(post.getFbFeedID()) + "");
			else holder.tvLikes.setText("");
		else
			Util.setLikeCount(post.getFbFeedID(), holder.tvLikes,likeCount);
		if (commentCount.containsKey(post.getFbFeedID()))
			if(commentCount.get(post.getFbFeedID())!=0) holder.tvComments
					.setText(commentCount.get(post.getFbFeedID()) + "");
			else holder.tvComments.setText("");
		else
			Util.setCommentCount(post.getFbFeedID(), holder.tvComments, commentCount);

		row.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.e("TEST: ", "position: " + position);
				Intent intent = new Intent(context, DetailNormalActivity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				DataHolder.setPost(posts.get(position));
				context.startActivity(intent);
			}
		});
		holder.layoutLikeBlog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickLike(post, holder.btnLike, holder.tvLikes);
			}
		});
		
		holder.layoutCommentBlog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CommentDialog.show((Activity) context, post.getFbFeedID(), new onCommentListener() {
					
					@Override
					public void onSuccess() {
						Util.setCommentCount(post.getFbFeedID(), holder.tvComments, commentCount);
					}
				});
			}
		});
		return row;
	}
	
	public void flushLikeCmtCount(){
		this.likeCount.clear();
		this.commentCount.clear();
	}
	
	public void setPostdata(ArrayList<Post> posts){
		this.posts = posts; 
	}

	private void onClickLike(final Post post, final ImageView btnLike, final TextView tvLikes) {
		if(post.isLiked()) {
		
			FbUtil.unLikeId(post.getFbFeedID(), (Activity) context, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int arg0, String arg1) {
				post.setLiked(false);
				btnLike.setBackgroundResource(R.drawable.like_black);
				tvLikes.setTextColor(Color.parseColor("#000000"));
				Util.setLikeCount(post.getFbFeedID(), tvLikes, likeCount);
				super.onSuccess(arg0, arg1);
			}
			
			@Override
			public void onFailure(Throwable arg0, String arg1) {
				super.onFailure(arg0, arg1);
			}
			
		});
			
		} else
		FbUtil.likeId(post.getFbFeedID(), (Activity) context,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, String response) {
						post.setLiked(true);
						btnLike.setBackgroundResource(R.drawable.like_red);
						tvLikes.setTextColor(Color.parseColor("#bf0000"));
						Util.setLikeCount(post.getFbFeedID(), tvLikes, likeCount);
						super.onSuccess(arg0, response);
					}

					@Override
					public void onFailure(Throwable arg0, String arg1) {
						super.onFailure(arg0, arg1);
						if(arg1!=null) Log.d("fbRes", arg1);
						Util.showToast("Lỗi kết nối", context,
								Toast.LENGTH_SHORT);
					}
				});
	}

	private class ViewHolder {
		 
		ImageView  btnLike,imgAvatar,imgDesc;
		TextView tvTitle, tvDesc, tvUserName, tvCreatedTime, tvLikes,
				tvComments, tvView;
		RelativeLayout layoutLikeBlog, layoutCommentBlog;
	}

}
